package com.didispace.chapter11.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.didispace.chapter11.service.CameraTemperatureAlarmService;
import com.didispace.chapter11.utils.DeviceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class CameraTemperatureAlarmServiceImpl implements CameraTemperatureAlarmService {
    @Async
    public void cameraTemperatureAlarmCheck(long timeInterval, String minuteFlag) {
        List<Map<String,String>> directoryName= new ArrayList<>();
        String path =File.separator+ "清涧亿蓬";
        String path2 =File.separator+ "copy_test";
        try {
            FTPClient ftp = DeviceUtil.getFTPClient("8.136.251.154", 21, "yunlan","YUN123lan456");
            DeviceUtil.getAllFileName(ftp,path,directoryName);

            if (directoryName.size()>0) {
                    for (Map<String, String> map : directoryName) {
                        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {

                            log.info("文件夹信息==={}", JSON.toJSONString(stringStringEntry));
                            boolean fileSaveFlag = true;//每个文件夹只需保留一次
                            if ("1".equals(stringStringEntry.getValue())){
                                    Map<String,String> namesMap = DeviceUtil.getNames(ftp,path+ File.separator+stringStringEntry.getKey());

                                    log.info("name.size==={}", namesMap.size());
//                                    log.info("name==={}", JSON.toJSONString(namesMap));

                                    LocalDateTime createTime = DateTime.now().toTimestamp().toLocalDateTime() ;
                                for (Map.Entry<String, String> stringEntry : namesMap.entrySet()) {
                                   String  name = stringEntry.getValue();
                                    if(! StrUtil.isBlank(name)){
                                        //文件名称里面取温度报警时间值 例如： 192.168.110.155_01_20211206085700484_MEASURE_TEMP_ALARM_DETECTION.jpg
                                        createTime = DateUtil.parse(StrUtil.split(name, StrUtil.C_UNDERLINE).get(2), DatePattern.PURE_DATETIME_MS_PATTERN).toTimestamp().toLocalDateTime();
                                        log.info("图片时间值： {}" , createTime);
                                    }

                                    String formatTime = createTime.format(DateTimeFormatter.ofPattern(DatePattern.PURE_TIME_PATTERN));

                                    //当前时间与图片时间的时间间隔
                                    long between = DateUtil.between(DateUtil.parseDateTime(createTime.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN))), new Date(), DateUnit.MINUTE);

                                    long matchMinuteMarkers =0;
                                    if(StrUtil.isNotEmpty(formatTime)){
                                        matchMinuteMarkers = Long.valueOf(formatTime.substring(2,4));
                                    }
                                    log.info("温度报警时间的分钟字符串值： {}分钟" , matchMinuteMarkers);
                                    if( fileSaveFlag && ( minuteFlag.equals(matchMinuteMarkers)) && (between> timeInterval)){
                                        log.info("复制文件到归档的文件夹，满足时间标记:{}" , matchMinuteMarkers);
                                        fileSaveFlag =false;
//                                        DeviceUtil.moveFile(ftp,path+ File.separator+stringStringEntry.getKey(),path2+ File.separator+stringStringEntry.getKey());
                                    }else{
                                        DeviceUtil.deleteByFolder(ftp,File.separator+name);
//                                        DeviceUtil.delete(path+ File.separator+name);//失效的写法
                                    }
                                }
                            }
                        }
            //            log.info("设置89天之前为逻辑删除标记,影响条数：{}", alarmListService.updateByCondition());//图片服务器上面的 每天跑一次 90天之前的图片删除
//                        DeviceUtil.makeDerectory(ftp,path);
                    }
                DeviceUtil.closeFTP(ftp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
